Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},
    hasUserInfo: false,
    score: '0.00'
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad() {
    const userInfo = wx.getStorageSync('userInfo')
    if (userInfo) {
      console.log('命中缓存');
      this.setData({
        userInfo: userInfo,
        hasUserInfo: true
      })
    }
  },
  getUserInfo() {
    if (!this.data.hasUserInfo) {
      wx.getUserProfile({
        desc: '展示用户信息',
        success: (res) => {
          console.log('设置缓存')
          const { userInfo } = res;
          wx.setStorageSync('userInfo', userInfo)
          this.setData({
            userInfo: userInfo,
            hasUserInfo: true
          })
        }
      })
    }
  },
  clearCache() {
    wx.removeStorageSync('userInfo')
    this.setData({
      hasUserInfo: false,
      userInfo: {}
    })
  },
})